<?php
// Tim'Roster
// Copyright (c) 2011 Nicolas Nallet <aspgic2@gmail.com>
//
// This file is part of Tim'Roster.
//
// Tim'Roster is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Tim'Roster is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA

$xml_config = simplexml_load_file('config.xml');

include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/class/Guild_Hall_Room.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/class/Enum/Enum_Item_Type.php');
include_once ($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/web_ui/common_function.php');


function render_item_list_sortby_class(array $item_array){
    $base_array = array();
    $fine_array = array();
    $choice_array = array();
    $xl_array = array();
    $sup_array = array();
    $content = '';

    if((isset($item_array)) && (count($item_array) > 0)){
        foreach($item_array as $item){
            switch ($item->get_item_class_id()) {
                case Enum_Item_Class::BASE_ITEM_CLASS_ID :
                    $base_array[] = $item;
                    break;
                case Enum_Item_Class::FINE_ITEM_CLASS_ID :
                    $fine_array[] = $item;
                    break;
                case Enum_Item_Class::CHOICE_ITEM_CLASS_ID :
                    $choice_array[] = $item;
                    break;
                case Enum_Item_Class::XL_ITEM_CLASS_ID :
                    $xl_array[] = $item;
                    break;
                case Enum_Item_Class::SUP_ITEM_CLASS_ID :
                    $sup_array[] = $item;
                    break;

                default:
                    $base_array[] = $item;
                    break;
            }
        }

        $content .= count($sup_array) > 0 ? '<h3>'.Language::get_instance()->get_label('lb_supreme').'</h3>'.  render_item_list($sup_array).'<br/>' : null;
        $content .= count($xl_array) > 0 ? '<h3>'.Language::get_instance()->get_label('lb_xl').'</h3>'.  render_item_list($xl_array).'<br/>' : null;
        $content .= count($choice_array) > 0 ? '<h3>'.Language::get_instance()->get_label('lb_choice').'</h3>'.  render_item_list($choice_array).'<br/>' : null;
        $content .= count($fine_array) > 0 ? '<h3>'.Language::get_instance()->get_label('lb_fine').'</h3>'.  render_item_list($fine_array).'<br/>' : null;
        $content .= count($base_array) > 0 ? '<h3>'.Language::get_instance()->get_label('lb_base').'</h3>'.  render_item_list($base_array).'<br/>' : null;
    }

    return $content;
}

function render_item_list(array $item_array){
    $content = '';

    for ($i = 0; $i < count($item_array); $i++) {
        $item = $item_array[$i];

        if ($item->get_quantity() > 999){
            $item_full_stacked = clone $item;
            $item_full_stacked->set_quantity(999);

            for ($j = 0; $j < floor($item->get_quantity()/999) ; $j++) {
                $content .= '<a href="#" class="tip_trigger"><img src="'.$item_full_stacked->get_icon().'" />'.generate_item_tooltip($item_full_stacked).'</a>';
            }

            if ($item->get_quantity()%999 != 0) {
                $item_not_full_stacked = clone $item;
                $item_not_full_stacked->set_quantity($item->get_quantity()%999);
                $content .= '<a href="#" class="tip_trigger"><img src="'.$item_not_full_stacked->get_icon().'" />'.generate_item_tooltip($item_not_full_stacked).'</a>';
            }

            
        }
        else {
            $content .= '<a href="#" class="tip_trigger"><img src="'.$item->get_icon().'" />'.generate_item_tooltip($item).'</a>';
        }
    }

    return $content;
}

function render_transaction_log(Guild_Hall_Room $room){

    $room->load_transaction_log();
    $trans_array = $room->get_transaction_log();
    $content = '';
    
    if (isset($trans_array)){
        foreach($trans_array as $trans){
            $content .= '<h2 class="trigger"><a href="#">'.Language::get_instance()->get_label('lb_transaction_of').' '.$trans->get_transaction_date().'</a></h2>
                <div class="toggle_container">
                <div class="block">';

            $deposit = $trans->get_deposited_item();
            $withdrawal = $trans->get_withdrawaled_item();

            if (count($deposit) > 0) {
                $content .= '<h2>'.render_transaction_type(Item_Transaction::TRANSACTION_TYPE_DEPOSIT_ID).Language::get_instance()->get_label('lb_deposit')
                                .' </h2>'.render_item_list_sortby_class($deposit);
                
                if (count($withdrawal) > 0){
                    $content .= '<br/><hr><br/>';
                }
            }

            if (count($withdrawal)){
                $content .= '<h2>'.render_transaction_type(Item_Transaction::TRANSACTION_TYPE_WITHDRAWAL_ID).Language::get_instance()->get_label('lb_withdrawal')
                                .' </h2>'.render_item_list_sortby_class($trans->get_withdrawaled_item());
            }
 
            $content .= '	</div></div>';
        }
    }

    return $content;
}

function render_transaction_type($trans_type) {
    $result = '<img src="';

    if ($trans_type == Item_Transaction::TRANSACTION_TYPE_DEPOSIT_ID){
        $result .= 'img/deposit.png';
    }
    else {
        $result .= 'img/withdrawal.png';
    }

    return $result.'" />';
}

function generate_item_tooltip(Item_Base $item){
    $tooltip = '<div class="tip"><b>'.$item->get_name().'</b>';
    
    $guild_name = $item->get_guild_name();
    if ((isset($guild_name)) && ($guild_name != '')){
        $tooltip .= '<br/>'.Language::get_instance()->get_label('lb_guild').' : '.$item->get_guild_name();
    }
   $tooltip .= ' <br/>'.Language::get_instance()->get_label('lb_class').' : '.$item->get_item_class_label().'
                <br/>'.Language::get_instance()->get_label('lb_quantity').' : '.$item->get_quantity().'
                <br/>'.Language::get_instance()->get_label('lb_quality').' : '.$item->get_quality();



    if ($item->get_item_type_id() == Enum_Item_Type::CRAFTED_ITEM_TYPE_ID){

        $tooltip .= '<table><tr><td valign="top">';

        $column_array = array();
        $column_array[] = '';
        $column_array[] = '';


        //durability & sapload
        $durability = $item->get_durability();
        if (isset($durability)){
            $tooltip .= '<fieldset class="tooltip"><legend>'.Language::get_instance()->get_label('lb_dura_n_sap').'</legend>';
           
            $tooltip .= generate_html_table_header($column_array, 173,false);
            $tooltip .= generate_html_table_center_row('<img class="icon" src="img/items_img/ICO_Preservation.png">', $item->get_healpoint().'/'.$durability);
 
            $sapload = $item->get_sapload();
            if (isset($sapload)){
                $tooltip .= generate_html_table_center_row('<img class="icon" src="img/items_img/ICO_Create_Sapload.png">',$item->get_current_sapload().'/'.$sapload);
            }

            $tooltip .= generate_html_table_footer();
            $tooltip .= '</fieldset>';
        }



        //bonus
        $hp_bonus = $item->get_healpoint_bonus();
        $sap_bonus = $item->get_sap_bonus();
        $stam_bonus = $item->get_stam_bonus();
        $focus_bonus = $item->get_focus_bonus();

        $bonus_array = generate_html_table_header($column_array, 173, false);
        $bonus_count = 0;
        if(isset($hp_bonus)){
            $bonus_array .= generate_html_table_center_row('<img class="icon" src="img/items_img/ICO_Heal.png">',$hp_bonus);
            $bonus_count++;
        }
        if (isset($sap_bonus)){
            $bonus_array .= generate_html_table_center_row('<img class="icon" src="img/items_img/ICO_Sap.png">',$sap_bonus);
            $bonus_count++;

        }
        if (isset($stam_bonus)){
            $bonus_array .= generate_html_table_center_row('<img class="icon" src="img/items_img/ICO_Stamina.png">',$stam_bonus);
            $bonus_count++;

        }
        if (isset($focus_bonus)){
            $bonus_array .= generate_html_table_center_row('<img class="icon" src="img/items_img/ICO_Focus.png">',$focus_bonus);
            $bonus_count++;
        }

        if($bonus_count > 0){

            $tooltip .= '<fieldset class="tooltip"><legend>'.Language::get_instance()->get_label('lb_bonus').'</legend>';
            $tooltip .= $bonus_array.generate_html_table_footer();
            $tooltip .= '</fieldset>';
        }

        //weapon stats
        $hit_rate = $item->get_hit_rate();
        if(isset($hit_rate)){
            $tooltip .= '<fieldset class="tooltip"><legend>'.Language::get_instance()->get_label('lb_weapon_stat').'</legend>';

            $tooltip .= generate_html_table_header($column_array, 173,false);
            $tooltip .= generate_html_table_center_row('<img class="icon" src="img/items_img/ICO_Hit_Rate.png">', $hit_rate.' / min');

            $range = $item->get_range();
            if (isset($range)){
                $tooltip .= generate_html_table_center_row('<img class="icon" src="img/items_img/ICO_Accurate.png">',$range.' m');
            }

            $tooltip .= generate_html_table_footer();
            $tooltip .= '</fieldset>';
        }
        
        $tooltip .= '</td><td valign="top">';

        $protect_factor = $item->get_prot_factor();

        if ((isset($protect_factor)) ||(isset($hit_rate))){
            //Dodge & Parry stats


            $tooltip .= '<fieldset class="tooltip"><legend>'.Language::get_instance()->get_label('lb_dodge_n_parry_modifier').'</legend>';

            $tooltip .= generate_html_table_header($column_array, 173, false);

            $tooltip .= generate_html_table_center_row('<img class="icon" src="img/items_img/ICO_Dodge.png" /> Perso', $item->get_dodge_modif());
            $tooltip .= generate_html_table_center_row('<img class="icon" src="img/items_img/ICO_Parry.png"> Perso', $item->get_parry_modif());

            if (isset($hit_rate)) {
                $tooltip .= generate_html_table_center_row('<img class="icon" src="img/items_img/ICO_Dodge.png" /> Adv', $item->get_adv_dodge_modif());
                $tooltip .= generate_html_table_center_row('<img class="icon" src="img/items_img/ICO_Parry.png"> Adv', $item->get_adv_parry_modif());
            }

            $tooltip .= generate_html_table_footer();

            $tooltip .= '</fieldset>';
        }

        

        //Armor protection
        if(isset($protect_factor)){
            $tooltip .= '<fieldset class="tooltip"><legend>'.Language::get_instance()->get_label('lb_protection').'</legend>';

            $tooltip .= generate_html_table_header($column_array, 173, false);
            $tooltip .= generate_html_table_center_row('<img class="icon" src="img/items_img/ICO_Armor.png">', $protect_factor.'%');
            $tooltip .= generate_html_table_center_row('<img class="icon" src="img/items_img/slashing.png">', $item->get_max_slash_prot());
            $tooltip .= generate_html_table_center_row('<img class="icon" src="img/items_img/piercing.png">', $item->get_max_pierce_prot());
            $tooltip .= generate_html_table_center_row('<img class="icon" src="img/items_img/blunt.png">', $item->get_max_blunt_prot());
            $tooltip .= generate_html_table_footer();
            $tooltip .= '</fieldset>';
        }

        $tooltip .= '</td></tr></table>';
    }


    $tooltip .= '</div>';

    return $tooltip;
}


function render_guild_hall_room_new(Guild_Hall_Room $room){
    $room->load_current_content();
   

    $content = '<h2 class="trigger"><a href="#">'.Language::get_instance()->get_label('lb_harvested_item').'</a></h2>
        <div class="toggle_container">
	<div class="block">';

    $content .= render_item_list_sortby_class($room->get_current_content_type_items(Enum_Item_Type::HARVESTED_ITEM_TYPE_ID));


    $content .= '	</div>
        </div>';



    $content .= '<h2 class="trigger"><a href="#">'.Language::get_instance()->get_label('lb_looted_item').'</a></h2>
        <div class="toggle_container">
	<div class="block">';

    $content .= render_item_list_sortby_class($room->get_current_content_type_items(Enum_Item_Type::LOOTED_ITEM_TYPE_ID));

    $content .= '	</div>
        </div>';



    $content .= '<h2 class="trigger"><a href="#">'.Language::get_instance()->get_label('lb_crafted_item').'</a></h2>
        <div class="toggle_container">
	<div class="block">';

    $content .= render_item_list_sortby_class($room->get_current_content_type_items(Enum_Item_Type::CRAFTED_ITEM_TYPE_ID));


    $content .= '	</div>
        </div>';


    $content .= '<h2 class="trigger"><a href="#">'.Language::get_instance()->get_label('lb_elevation_crystal_item').'</a></h2>
        <div class="toggle_container">
	<div class="block">';

    $item_array = $room->get_current_content_type_items(Enum_Item_Type::ELEVATION_CRYSTAL_ITEM_TYPE_ID);
     for ($i = 0; $i < count($item_array); $i++) {
         $content .= render_elevation_crystal($item_array[$i]);
     }

    $content .= '	</div>
        </div>';


    return $content;
}

function render_elevation_crystal(Item_Base $item){

    $icon = substr($item->get_icon(), 0, strpos($item->get_icon(), '&s='));

    return $content = '<a href="#" class="tip_trigger"><img src="'.$icon.'" />'.generate_item_tooltip($item).'</a>  x '.$item->get_quantity().' ('.ceil($item->get_quantity()/999).' stacks)<br/>';
}

?>
